﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;

using ihongma.WebData;

namespace ihongma.test
{
    public partial class u148 : System.Web.UI.Page
    {
        JumonyParser parser = new JumonyParser();
        protected void Page_Load(object sender, EventArgs e)
        {
           // pageinfo("http://www.u148.net/article/83657.html", "2013-03-15");
           // url(2);
            for (int i = 0; i <= 50; i++)
            {
                url(i);
            }
        }
        public void url(int j)
        {
            //StringBuilder sb = new StringBuilder();
            string showhtml = HttpRequestHelper.GetPageSourceByUrl("http://www.u148.net/audio/" + j.ToString() + ".html", "utf-8");
            var dataDocument = parser.Parse(showhtml);
           
            dataDocument.Find(".summary .level4").Remove();
             dataDocument.Find(".summary .comments").Remove();
             dataDocument.Find(".u148content h1 .link1").Remove();
            var date = dataDocument.Find(".summary span").ToArray();
      
             var items = dataDocument.Find(".u148content h1 a").ToArray();
        // Response.Write(items.Count().ToString() + "dsd" + date.Count());
             for (int i = 0; i < items.Count(); i++)
             {
                 if (date[i].InnerHtml() != "")
                 {
                     // Response.Write("http://www.u148.net/" + items[i].Attribute("href").Value());
                     pageinfo("http://www.u148.net" + items[i].Attribute("href").Value(), date[i].InnerHtml().Replace("发布于", ""));
                 }
                 else
                 {
                     pageinfo("http://www.u148.net" + items[i].Attribute("href").Value(), DateTime.Now.ToString());
                 }
                 //href.Count()
             }
             //foreach (var i in date.ToArray())
             //{
             //    Response.Write(i.InnerHtml().Replace("发布于", "") + "<br/>");
             //}
         

        }
        public string ret(string str)
        {
            string temp=str;
            if (str.Contains("http://file3.u148.net"))
            {
                temp = temp.Replace("http://file3.u148.net", "/tools/imgdow.ashx?URL=http://file3.u148.net");
            }

            if(str.Contains("http://file1.u148.net"))
            {
                temp = temp.Replace("http://file2.u148.net", "/tools/imgdow.ashx?URL=http://file2.u148.net");
            }
          if(str.Contains("http://file1.u148.net"))
            {
                temp = temp.Replace("http://file1.u148.net", "/tools/imgdow.ashx?URL=http://file1.u148.net");
            }
            return temp;
        }
        public void pageinfo(string usrl, string data)
        {
            //StringBuilder sb = new StringBuilder();
            string showhtml = HttpRequestHelper.GetPageSourceByUrl(usrl, "utf-8");
            var dataDocument = parser.Parse(showhtml);
            var items = dataDocument.Find(".u148content");
            var conunt =ret(items.Find(".content").Single().ToString().Replace(usrl, "#").Replace("来自有意思吧（www.u148.net）", "来自鸿马网（www.ihongma.com）").Replace("www.u148.net", "www.ihongma.com"));
            var titele = items.Find("h1 .link2").Single().InnerText();
            var img = "";
            var temp = dataDocument.Find("meta[name='Description']").Single().Attribute("content").Value();
            try
            {
                img ="/tools/imgdow.ashx?URL="+items.Find(".content img").ElementAt(0).Attribute("src").Value();
            }
            catch (Exception)
            {

                img = "/upload/null.jpg";
            }
            com.ihongma.www.addserver add = new com.ihongma.www.addserver();
            com.ihongma.www.article ar = new com.ihongma.www.article()
            {
                add_time = DateTime.Parse(data),
                author = "colt",
                category_id = 353,
                channel_id = 1,
                zhaiyao = temp,
                click = 100,
                content = conunt,
                //dataDocument.Find("#artical_real p").ElementAt(0).InnerHtml() + "<p>" + temp + "</p>",

                title = titele,
                digg_act = 0,
                digg_good = 0,
                form = "鸿马",
                img_url = img,
                is_hot = 0,
                is_lock = 0,
                is_msg = 0,
                is_red = 0,
                is_slide = 0,
                is_top = 0,
                seo_description = temp,
                seo_keywords = titele,
                seo_title = titele,
                sort_id = 99,
                link_url = ""

            };
            add.addnews(ar, "s123456");

        }
    }
}